﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FantastPlatform.Community.Model;
using FantastPlatform.Community.IMode;
using FantastPlatform.Framework.Service;
using FantastPlatform.Community.IService;
using NHibernate;
using FantastPlatform.Framework;
using FantastPlatform.Framework.Core;
using NHibernate.Criterion;
using FantastPlatform.Framework.IModel;

namespace FantastPlatform.Community.Service
{
    public class WorkLogService : DataService<IWorkLog, WorkLog>, IWorkLogService
    {
        public void DeleteWorkLog(IList<int> ids)
        {
            ITransaction transaction = null;
            ExecuteTransaction((ITransaction tan) =>
            {
                foreach (int id in ids)
                {
                    IWorkLogReplyService workLogReplyService = ServiceFactory.CreateService<IWorkLogReplyService>();
                    ConditionGroup group = new ConditionGroup();
                    group.Conditions.Add(new Condition { Field = "Enable", Type = typeof(bool).FullName, Value1 = true });
                    group.Conditions.Add(new Condition { Field = "WorkLog.Id", Type = typeof(Int32).FullName, Value1 = id });
                    int totalCount = 0;
                    IList<IWorkLogReply> list = workLogReplyService.Search(group, null, Int32.MaxValue, 1, out totalCount);
                    if (totalCount > 0)
                    {
                        foreach (IWorkLogReply item in list)
                        {
                            workLogReplyService.Delete(item.Id);
                        }
                    }
                    base.Delete(id);
                }
            }, transaction);
        }

        public IList<IWorkLog> GetPromptInfo(int policeId)
        {
            ICriteria criteria = Session.CreateCriteria(typeof(WorkLog)).CreateAlias("PoliceMan", "p")
                    .Add(Expression.Eq("p.Id", policeId)).Add(Expression.Eq("Enable", true))
                    .Add(Expression.Gt("PromptTime", DateTime.Now.Date)).Add(Expression.Eq("IsPrompt", true));
            IList<IWorkLog> worklog = criteria.List<IWorkLog>();

            //加入房屋到期提醒
            ICriteria holdCriteria = Session.CreateCriteria(typeof(HouseHold)).CreateAlias("HouseDistrict", "h")
                .CreateAlias("h.PoliceMan", "p").Add(Expression.Eq("p.Id", policeId))
                .Add(Expression.Eq("Enable", true))
                .Add(Expression.Lt("Deadline", DateTime.Now.AddDays(7)))
                .CreateAlias("Purposes", "ps").Add(Expression.Eq("ps.Name", "出租"));
            IList<IHouseHold> hlist = holdCriteria.List<IHouseHold>();
            for (int i = 0; i < hlist.Count; i++)
            {
                IHouseHold hold = hlist[i];
                IHouseDistrict hd = hlist[i].HouseDistrict;
                worklog.Add(new WorkLog
                {
                    Name = "房屋到期提醒",
                    Content = hd.Street.Name + hd.Address + hold.Number + "房屋到期提醒",
                    PromptTime = hlist[i].Deadline
                });
            }

            return worklog;
        }
    }
}
